﻿package com.game.view.layer
{
    import com.dusk.util.FilterUtil;
    import com.dusk.util.ResUtil;
    import com.dusk.util.StringUtil;
    import com.dusk.util.TextUtil;
    import com.game.manager.GameSceneManager;
    import com.game.manager.NotificationManager;
    
    import flash.display.*;
    import flash.events.*;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.ui.Mouse;
    import flash.ui.MouseCursor;
    import flash.utils.ByteArray;
    
    public class SelectArchive extends LayerBase
	{
		
		public function SelectArchive(param1:String = "read")
		{
			super();
			state = param1;
		}
		
		public var state:String = "";
		public var btn_x:SimpleButton;
		public var btn_1:MovieClip;
		public var btn_2:MovieClip;
		public var btn_3:MovieClip;
		public var btn_4:MovieClip;
		public var btn_5:MovieClip;
		public var btn_0:MovieClip;
		public var infoTxt:TextField;
		private var isCover:Sprite;
		private var index:int;
		
		private var _gameData:ByteArray;
		
		private function get gameData():Object
		{
			_gameData.position = 0;
			return _gameData.readObject();
		}
		
		private function set gameData(param1:Object):void
		{
			if (!_gameData)
			{
				_gameData = new ByteArray();
			}
			_gameData.writeObject(param1);
		}
		
		public function clearTxt():void
		{
			for (var i:int = 0; i <= 5; i++)
			{
				this["btn_" + i].txt_name.text = "空存档";
				this["btn_" + i].txt_time.text = "";
			}
		}
		
		private function redrawUI():void
		{
			for (var i:int = 0; i <= 5; i++)
			{
				TextUtil.redrawTxtOld(this["btn_" + i], ["txt_name", "txt_time"]);
			}
		}
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("SelectArchive") as MovieClip;
			for (var i:int = 0; i <= 5; i++)
			{
				this["btn_" + i] = mc.getChildByName("btn_" + i) as MovieClip;
			}
			btn_x = mc.getChildByName("btn_x") as SimpleButton;
			infoTxt = TextUtil.creatTextField("",TextUtil.UNIVERSAL_FONT,20,TextFormatAlign.CENTER,0xFFFFFF,false,true);
			infoTxt.x = 290;
			infoTxt.y = 140;
			infoTxt.width = 350;
			infoTxt.height = 270;
			mc.addChild(infoTxt);
			addChild(mc);
			redrawUI();
			clearTxt();
		}
		
		override protected function onAdded():void
		{
			if (!isCover)
			{
				isCover = ResUtil.getComponentView("IsCover") as Sprite;
			}
			if (gc.isNew)
			{
				infoTxt.text = "第一次进入游戏请选择存档";
			}
			for (var i:int = 0; i <= 5; i++)
			{
				with (this["btn_" + i])
				{
					buttonMode = true;
					addEventListener(MouseEvent.CLICK, onClick);
					addEventListener(MouseEvent.ROLL_OVER, over);
					addEventListener(MouseEvent.ROLL_OUT, out);
				}
			}
			setCloseBtn(btn_x);
			refreshList();
		}
		
		override protected function onRemoved():void
		{
			for (var i:int = 0; i <= 5; i++)
			{
				with (this["btn_" + i])
				{
					buttonMode = false;
					removeEventListener(MouseEvent.CLICK, onClick);
					removeEventListener(MouseEvent.ROLL_OVER, over);
					removeEventListener(MouseEvent.ROLL_OUT, out);
				}
			}
		}
		
		private function refreshList():void
		{
			var _loc1_:int = 0;
			var _loc2_:TextFormat = null;
			gameData = gc.memory.getAllSaveData();
			while (_loc1_ < 6)
			{
				this["btn_" + _loc1_].txt_name.text = "空存档";
				this["btn_" + _loc1_].txt_time.text = "";
				_loc1_++;
			}
			for (_loc1_ = 0; _loc1_ < 6; _loc1_++)
			{
				if (gameData[_loc1_])
				{
					this["btn_" + _loc1_].txt_name.text = SaveUtils.getRoleNameById(gameData[_loc1_]["player1_obj"]["roleId"]) + "(" + gameData[_loc1_]["player1_obj"]["level"] + "级)";
					_loc2_ = this["btn_" + _loc1_].txt_name.defaultTextFormat;
					if (gameData[_loc1_].playerNum - 1)
					{
						if (!gameData[_loc1_].player2_obj)
						{
							throw "invalid data";
						}
						_loc2_.size = 17;
						this["btn_" + _loc1_].txt_name.text += " " + SaveUtils.getRoleNameById(gameData[_loc1_]["player2_obj"]["roleId"]) + "(" + gameData[_loc1_]["player2_obj"]["level"] + "级)";
						this["btn_" + _loc1_].txt_name.setTextFormat(_loc2_);
					}
					if (gameData[_loc1_].saveDate)
					{
						this["btn_" + _loc1_].txt_time.text = gameData[_loc1_]["saveDate"];
					}
					else
					{
						this["btn_" + _loc1_].txt_time.text = "null";
					}
				}
			}
		}
		
		private function okClick(evt:MouseEvent):void
		{
			isCover["btn_ok"].removeEventListener(MouseEvent.CLICK, okClick);
			isCover["btn_change"].removeEventListener(MouseEvent.CLICK, changeClick);
			if (getChildByName("isCover") != null)
			{
				removeChild(isCover);
			}
			gc.memory.saveGame(index);
			NotificationManager.alert("存档成功！请重新登录！");
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MENU);
			destroy();
		}
		
		private function changeClick(evt:MouseEvent):void
		{
			isCover["btn_ok"].removeEventListener(MouseEvent.CLICK, okClick);
			isCover["btn_change"].removeEventListener(MouseEvent.CLICK, changeClick);
			if (getChildByName("isCover") != null)
			{
				removeChild(isCover);
			}
		}
		
		private function destroy():void
		{
			if (contains(isCover))
			{
				removeChild(isCover);
			}
			removeFromParent();
			_gameData = null;
			isCover = null;
		}
		
		private function over(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.BUTTON;
			FilterUtil.setGlow(param1.target,0xFFFFFF,1,8,8,1.5,3);
			FilterUtil.clearCache();
		}
		
		private function out(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.AUTO;
			FilterUtil.deleteAllFilters(param1.target);
		}
		
		private function onClick(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.AUTO;
			index = int(StringUtil.getLastMultiChars(param1.currentTarget.name));
			if (state == "save")
			{
				if (param1.currentTarget.txt_name.text != "空存档")
				{
					addChild(isCover);
					isCover.name = "isCover";
					isCover["btn_ok"].addEventListener(MouseEvent.CLICK, okClick);
					isCover["btn_change"].addEventListener(MouseEvent.CLICK, changeClick);
				}
				else
				{
					gc.memory.saveGame(index);
					NotificationManager.alert("存档成功！");
					destroy();
					GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MENU);
				}
			}
			else if (state == "read")
			{
				var _parent:GameMenu = parent as GameMenu;
				if (param1.currentTarget.txt_name.text != "空存档")
				{
					gc.memory.storageValue(gameData[index]);
					gc.saveId = index;
					if (_parent != null)
					{
						_parent.showAndHide();
					}
					destroy();
				}
				else if (parent.hasOwnProperty("showSelectNum"))
				{
					gc.newGameClick();
					gc.isNew = true;
					if (_parent != null)
					{
						_parent.showSelectNum();
					}
					destroy();
				}
			}
		}
	}
}

class SaveUtils
{
	public static function getRoleNameById(param1:int):String
	{
		switch (param1)
		{
			case 1:
				return "悟空";
			case 2:
				return "唐僧";
			case 3:
				return "八戒";
			case 4:
				return "沙僧";
			default:
				return "";
		}
	}
}